﻿using System.Collections.Generic;

namespace Ruminate.DataStructures {

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class TreeNode<T> {

        /// <summary>
        /// 
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public TreeNode<T> Parent { get; set; }
        public List<TreeNode<T>> Children { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public TreeNode(T data) {

            Data = data;
            Children = new List<TreeNode<T>>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="child"></param>
        public void AddChild(TreeNode<T> child) {

            child.Parent = this;            
            Children.Add(child);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        public void RemoveChild(T data) {

            foreach (var treeNode in Children) {
                
                if (treeNode.Data.Equals(data)) {
                    RemoveChild(treeNode);
                    return;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="child"></param>
        public void RemoveChild(TreeNode<T> child) {

            if (child.Parent == this && Children.Contains(child)) {
                child.Parent = null;
                Children.Remove(child);
            }
        }
    }
}
